﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using AlIftaaGateDBIbnBazBusinessLayer;
using AlIftaaGateDBIbnBazDataLayer;
using AdoHelper;
using System.Net;
using System.IO;
using System.Threading;
using System.Text;
using System.Text.RegularExpressions;

namespace ExtractToDataBase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            AlIftaaGateDBIbnBazDataLayer.DataBaseConnections.AlIftaaGateDBIbnBaz = new SqlDataBase(".", "AlIftaaGateDBIbnBaz");
        }

        private bool continueFlag=true;
        private void button1_Click(object sender, EventArgs e)
        {
            for (int i = 1; i < 6005; i++)
            {
                BLPage page = new BLPage();
                page.ID = i;
                page.LoadFromDataBase();
                continueFlag = false;
                this.webBrowser1.Navigate(string.Format("http://127.0.0.1:49000/WebInterface/DisplayChap.aspx?View=Page&PageID={0}&PageNo=1&BookID=4", i));
                continueFlag = false;
                while (!continueFlag)
                {
                    Application.DoEvents();
                }
                webBrowser1.Document.Encoding = Encoding.UTF8.WebName;
                HtmlElement htmlelement = this.webBrowser1.Document.GetElementById("table1").Children[0].Children[1].Children[0].Children[0].Children[0].Children[1].Children[0].Children[1].Children[0].Children[1].Children[0].Children[0].Children[0].Children[0].Children[0];
                string s = htmlelement.InnerText.Replace(page.Title,"");
                s = Regex.Replace(s, @"الجزء رقم\s*:\s*[0-9]+", "");
                s = Regex.Replace(s, @"الصفحة رقم\s*:\s*[0-9]+", "");
                s = s.Replace("(  ،  )", "");
                page.DisplayContent = s.Trim();
                s = Regex.Replace(s, "[أآإ]", "ا");
                s = Regex.Replace(s, "ة", "ه");
                s = Regex.Replace(s, "ى", "ي");
                s = Regex.Replace(s, @"[َُِّْ~ًٌٍ]", "");//علامات الترقيم
                s = Regex.Replace(s, @"\bعبد\b\s*", " عبد");
                s = Regex.Replace(s, @"\bابو\b\s*", " ابو");
                s = Regex.Replace(s, @"\W", " ");
                s = Regex.Replace(s,@"\s+"," ");
                page.SearchContent = " " + s.Trim() + " ";
                page.SaveToDataBase(SaveToDataBaseType.Update);
            }
        }

        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            continueFlag = true;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29, new FileInfo("d:\\noise.dat"));
            Lucene.Net.Store.Directory directory = Lucene.Net.Store.FSDirectory.Open(new DirectoryInfo("d:\\LuceneIndex"));
            Lucene.Net.Index.IndexWriter writer = new Lucene.Net.Index.IndexWriter(directory, analyzer, true, Lucene.Net.Index.IndexWriter.MaxFieldLength.UNLIMITED);

            BLPageCollection pageCollection = new BLPageCollection();
            Exception error = pageCollection.LoadFromDataBase();
            for (int i = 0; i < pageCollection.Count; i++)
            {
                Lucene.Net.Documents.Document document = new Lucene.Net.Documents.Document();
                document.Add(new Lucene.Net.Documents.Field("ID", pageCollection[i].ID.ToString(), Lucene.Net.Documents.Field.Store.YES, Lucene.Net.Documents.Field.Index.NO));
                document.Add(new Lucene.Net.Documents.Field("CONTENT", pageCollection[i].DisplayContent, Lucene.Net.Documents.Field.Store.NO, Lucene.Net.Documents.Field.Index.ANALYZED));
                writer.AddDocument(document);
                this.textBox1.Text = i.ToString();
            }
            MessageBox.Show("finish");

        }

        private void button3_Click(object sender, EventArgs e)
        {
            Lucene.Net.Analysis.Analyzer analyzer = new Lucene.Net.Analysis.Standard.StandardAnalyzer(Lucene.Net.Util.Version.LUCENE_29, new FileInfo("d:\\noise.dat"));

            LuceneHelper.SearchHelper searchHelper = new LuceneHelper.SearchHelper();
            searchHelper.NoiseDataPath = "";
            LuceneHelper.DocumentsHelper documents = searchHelper.Search(new string[] { "d:\\LuceneIndex" }, "الربا", analyzer, null, null, "CONTENT");
            for (int i = 0; i < documents.Count; i++)
            {
                MessageBox.Show(documents[i,"ID"]);
            }
            MessageBox.Show(documents.Errors[0].Message);
        }
    }
}
